Access Control

Access Control Syntax

                  public class SomePublicClass {}

internal class SomeInternalClass {}

fileprivate class SomeFilePrivateClass {}

private class SomePrivateClass {}



public var somePublicVariable = 0

internal let someInternalConstant = 0

fileprivate func someFilePrivateFunction() {}

private func somePrivateFunction() {}
                
                  class SomeInternalClass {}              // implicitly internal

let someInternalConstant = 0            // implicitly internal
                

Custom Types

                  public class SomePublicClass {                  // explicitly public class

    public var somePublicProperty = 0            // explicitly public class member

    var someInternalProperty = 0                 // implicitly internal class member

    fileprivate func someFilePrivateMethod() {}  // explicitly file-private class member

    private func somePrivateMethod() {}          // explicitly private class member

}



class SomeInternalClass {                       // implicitly internal class

    var someInternalProperty = 0                 // implicitly internal class member

    fileprivate func someFilePrivateMethod() {}  // explicitly file-private class member

    private func somePrivateMethod() {}          // explicitly private class member

}



fileprivate class SomeFilePrivateClass {        // explicitly file-private class

    func someFilePrivateMethod() {}              // implicitly file-private class member

    private func somePrivateMethod() {}          // explicitly private class member

}



private class SomePrivateClass {                // explicitly private class

    func somePrivateMethod() {}                  // implicitly private class member

}
                

Function Types

                  func someFunction() -> (SomeInternalClass, SomePrivateClass) {

    // function implementation goes here

}
                
                  private func someFunction() -> (SomeInternalClass, SomePrivateClass) {

    // function implementation goes here

}
                

Enumeration Types

                  public enum CompassPoint {

    case north

    case south

    case east

    case west

}
                

Subclassing

                  public class A {

    fileprivate func someMethod() {}

}



internal class B: A {

    override internal func someMethod() {}

}
                
                  public class A {

    fileprivate func someMethod() {}

}



internal class B: A {

    override internal func someMethod() {

        super.someMethod()

    }

}
                

Constants, Variables, Properties, and Subscripts

                  private var privateInstance = SomePrivateClass()
                

Getters and Setters

                  struct TrackedString {

    private(set) var numberOfEdits = 0

    var value: String = "" {

        didSet {

            numberOfEdits += 1

        }

    }

}
                
                  var stringToEdit = TrackedString()

stringToEdit.value = "This string will be tracked."

stringToEdit.value += " This edit will increment numberOfEdits."

stringToEdit.value += " So will this one."

print("The number of edits is \(stringToEdit.numberOfEdits)")

// Prints "The number of edits is 3"
                
                  public struct TrackedString {

    public private(set) var numberOfEdits = 0

    public var value: String = "" {

        didSet {

            numberOfEdits += 1

        }

    }

    public init() {}

}
                

Private Members in Extensions

                  protocol SomeProtocol {

    func doSomething()

}
                
                  struct SomeStruct {

    private var privateVariable = 12

}



extension SomeStruct: SomeProtocol {

    func doSomething() {

        print(privateVariable)

    }

}